package com.freemarker;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;

public class TestController{

	public static void main(String[] args) throws Exception{
		
		//基础数据封装
		Map<String, String> root = new HashMap<>();
		
		setParam(root);
		
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
		File file = new File(root.get("loadPath"));
		if(!file.exists()) {
			file.mkdir();
		}
        cfg.setDirectoryForTemplateLoading(file);    
        cfg.setDefaultEncoding("UTF-8");
        cfg.setClassicCompatible(true);
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        
        product(root, cfg);
        
        
        
	}
	
	//输出代码
	private static void product(Map<String, String> root, Configuration cfg) throws TemplateNotFoundException,
			MalformedTemplateNameException, ParseException, IOException, TemplateException {
		if("1".equals(root.get("setBatch"))) {
			//TODO 这里批量生成还需要考虑一下 
		}else {
			//controller
			 File controllerFile = new File(root.get("prodRootPath")+root.get("className")+"Controller.java");
		     if(!controllerFile.exists()) {
	        	Template temp = cfg.getTemplate("controller.ftl");
	            FileWriter fileOut = new FileWriter(controllerFile);
	            temp.process(root, fileOut);
	            fileOut.flush();
	            fileOut.close();
		     }

	        //服务实现层
	        File serviceImplFile = new File(root.get("prodRootPath")+root.get("className")+"ServiceImpl.java");
	        if(!serviceImplFile.exists()) {
	        	Template temp = cfg.getTemplate("service-impl.ftl");
	        	FileWriter fileOut = new FileWriter(serviceImplFile);
	        	temp.process(root, fileOut);
	        	fileOut.flush();
	        	fileOut.close();
	        }
		     
			//服务层
			File serviceFile = new File(root.get("prodRootPath")+root.get("className")+"Service.java");
	        if(!serviceFile.exists()) {
	        	Template temp = cfg.getTemplate("service.ftl");
	            FileWriter fileOut = new FileWriter(serviceFile);
	            temp.process(root, fileOut);
	            fileOut.flush();
	            fileOut.close();
	        }
	        //映射层
	        File mapperFile = new File(root.get("prodRootPath")+root.get("className")+"Mapper.java");
	        if(!mapperFile.exists()) {
	        	Template temp = cfg.getTemplate("mapper.ftl");
	        	FileWriter fileOut = new FileWriter(mapperFile);
	        	temp.process(root, fileOut);
	        	fileOut.flush();
	        	fileOut.close();
	        }
	        
		}
	}
	
	//基础数据封装
	private static void setParam(Map<String, String> root) {
		
		ResourceBundle bundle = PropertyResourceBundle.getBundle("config");
		
		//基础类路径
        String className =bundle.getString("mt.class.name");
        String note =bundle.getString("mt.note");
        try {
        	note = new String(note.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        String classNames =bundle.getString("mt.class.names");
        String notes =bundle.getString("mt.notes");
        String projectName =bundle.getString("mt.project.name");
        String model=bundle.getString("mt.model");
        String service=bundle.getString("mt.service");
        String impl=bundle.getString("mt.service.impl");
        String controller=bundle.getString("mt.controller");
        String author=bundle.getString("mt.author");
        String loadPath=bundle.getString("mt.load.path");
        String prodRootPath=bundle.getString("mt.prod.root.path");
        String setPath=bundle.getString("mt.set.path");
        String setBatch=bundle.getString("mt.set.batch");
        String mapper=bundle.getString("mt.mapper");
        
        root.put("projectName", projectName);
        root.put("model", model);
        root.put("service", service);
        root.put("impl", impl);
        root.put("controller", controller);
        root.put("loadPath", loadPath);
        root.put("prodRootPath", prodRootPath);
        root.put("setPath", setPath);
        root.put("setBatch", setBatch);
        root.put("mapper", mapper);
        
		//引用包路径
		root.put("controllerPackageName",projectName+"."+controller);
        root.put("servicePackageName",projectName+"."+service);
        root.put("servicePackageName",projectName+"."+service);
        root.put("serviceImplPackgeName", projectName+"."+service+"."+impl);
        root.put("modelPackgeName", projectName+"."+model);
        root.put("mapperPackgeName", projectName+"."+mapper);
        
        //类路径
       /* root.put("controllerClassPath",projectName+"."+controller+"."+className+"Controller.java");
        root.put("serviceImplClassPath", projectName+"."+service+"."+impl+"."+className+"ServiceImpl.java");
        root.put("serviceClassPath",projectName+"."+service+"."+className+"Service.java");
        root.put("mapperClassPath", projectName+"."+mapper+"."+className+"Mapper.java");
        root.put("modelClassPath", projectName+"."+model+"."+className+".java");*/
        
        //类名
        root.put("controllerClassName",projectName+"."+controller+"."+className+"Controller");
        root.put("serviceImplClassName", projectName+"."+service+"."+className+"ServiceImpl");
        root.put("serviceClassName",projectName+"."+service+"."+className+"Service");
        root.put("mapperClassName",projectName+"."+service+"."+className+"Mapper");
        root.put("modelClassName", projectName+"."+model+"."+className);
        
        
        //
        root.put("author", author);
        root.put("className", className);
        root.put("note", note);
        root.put("classNames", classNames);
        root.put("notes", notes);
        root.put("notes", notes);
	}
	
}
